package com.hinata.hinataimageloader;

import android.app.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import java.io.File;


public class ImageChoiceActivity extends Activity implements OnClickListener {

    private static final int RESULT_IMAGE=100;
    //设置MIME码：表示image所有格式的文件均可
    private static final String IMAGE_TYPE="image/*";


    //设置返回码：标志照相机拍摄
    private static final int RESULT_CAMERA=200;
    //设置一个临时路径，保存所拍的照片
    private static String TEMP_IMAGE_PATH;



    private Button btnHomeworkTakePhoto, btnHomeworkPickPhoto, btnHomeworkCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_choice);

        btnHomeworkTakePhoto = (Button) this.findViewById(R.id.btn_homework_take_photo);
        btnHomeworkPickPhoto = (Button) this.findViewById(R.id.btn_homework_pick_photo);
        btnHomeworkCancel = (Button) this.findViewById(R.id.btn_homework_cancel);

        btnHomeworkTakePhoto.setOnClickListener(this);
        btnHomeworkPickPhoto.setOnClickListener(this);
        btnHomeworkCancel.setOnClickListener(this);

    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_homework_take_photo:

                //获取该路径
                TEMP_IMAGE_PATH= Environment.getExternalStorageDirectory().getPath()+"/temp.png";
                //传入ACTION_IMAGE_CAPTURE:该action指向一个照相机app
                Intent intent1=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //创建File并获取它的URI值
                Uri photoUri=Uri.fromFile(new File(TEMP_IMAGE_PATH));
                //MediaStore.EXTRA_OUTPUT为字符串"output"，即将该键值对放进intent中
                intent1.putExtra(MediaStore.EXTRA_OUTPUT,photoUri);
                startActivityForResult(intent1,RESULT_CAMERA);




                break;
            case R.id.btn_homework_pick_photo:


                //实例化Intent,传入ACTION_PICK,表示从Item中选取一个数据返回
                Intent intent=new Intent(Intent.ACTION_PICK,null);
                //设置Data和Type属性，前者是URI：表示系统图库的URI,后者是MIME码
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_TYPE);
                //启动这个intent所指向的Activity
                startActivityForResult(intent,RESULT_IMAGE);



                break;
            case R.id.btn_homework_cancel:

                break;
            default:
                break;
        }
    }



}
